package com.hnas.common.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hnas.common.domain.BusinessType;
import com.hnas.common.service.ImportDataPoolService;
import com.hnas.common.service.dto.DTOImportDataPool;
import com.hnas.common.service.dto.search.DTOImportDataPoolSearch;
import com.hnas.core.dto.DTOPagedList;
import com.hnas.core.dto.DataBean;

@Controller
@RequestMapping("/backend/common/importDataPool")
public class ImportDataPoolControl {
	
	private static Log log = LogFactory.getLog(ImportDataPoolControl.class);

	@Autowired
	private ImportDataPoolService importDataPoolService;
	
	@RequestMapping("/search")
	public String search(ModelMap modelMap, HttpServletRequest request,
			@ModelAttribute("criteria") DTOImportDataPoolSearch criteria) {
		try {
			criteria.setTotalCount(-1); 
			DTOPagedList<DTOImportDataPool> pg = importDataPoolService.search(criteria);
			modelMap.put("pg", pg);
			modelMap.put("busTypes", BusinessType.getBusinessTypeMap());
			modelMap.put("optTypes", BusinessType.getOpTypeMap());
			modelMap.put("dto", criteria);
		} catch (Exception e) {
			log.error("", e);
			modelMap.put("resultSize", 0);
		}
		modelMap.put("pageTitle", "批量导入详情");
		List<DataBean> beans = new ArrayList<DataBean>();
		beans.add(new DataBean("businessTypeStr","businessTypeStr","导入类型",false,150));
		beans.add(new DataBean("fileName","fileName","文件名",false,150));
		beans.add(new DataBean("totalCount","totalCount","总条数",false,150));
		beans.add(new DataBean("successCount","successCount","成功条数",false,150));
		beans.add(new DataBean("failedCount","failedCount","失败条数",false,150));
		beans.add(new DataBean("createTime","createTime","开始时间",false,150));
		beans.add(new DataBean("finishTime","finishTime","结束时间",false,150));
		beans.add(new DataBean("chooseParam","chooseParam","参数",false,150));
		beans.add(new DataBean("opTypeStr","opTypeStr","操作类型",false,150));
		modelMap.put("beans", beans);
		return "backend/common/listImportDataPool";
	}
	
	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public BaseAjaxResponse save( DTOImportDataPool dto) {
		BaseAjaxResponse res = new BaseAjaxResponse();
		this.importDataPoolService.saveImportDataPool(dto);
		res.setResult(1);
		return res;
	}
	
	@RequestMapping(value = "/disable/{id}", method = RequestMethod.POST)
	@ResponseBody
	public BaseAjaxResponse disable(@PathVariable("id") Long id) {
		BaseAjaxResponse res = new BaseAjaxResponse();
		this.importDataPoolService.disableImportDataPool(id);
		res.setResult(1);
		return res;
	}
	
}
